<?php
class BookdealController extends BaseController
{	
	public $book_id = 1;//标识要处理的书
	// public $file =   BASE_DIR . 'protected/data/now_book.html'; //去样式后的源html
	// public $source =  BASE_DIR . 'protected/data/book1.html'; //去样式后的源html
	
	public function actionIndex(){
		echo '导入相关';
		//model('func')->
	}
	
	//后期数据处理 1-> url替换 2->前后章节处理 3,数据库去span标签
	public function actionUrl(){
		echo 'url替换处理';
		$res = obj('Book')->select(array('book_id'=>$this->book_id),'id,section_id,content_en','id ASC');
		foreach($res as $k=>$v){
			$data = str_ireplace('#LinkTarget_','http://yishouce.com/book/' . $this->book_id . '/',$v['content_en']);
			obj('Book')->update(array('id'=>$v['id']),array('content_en'=>$data));
			//if($k>6) return;
		}
	}
	//span标签去除
	public function actionSpan(){
		echo 'span替换处理';
		$res = obj('Book')->select(array('book_id'=>$this->book_id),'id,section_id,content_en','id ASC');
		foreach($res as $k=>$v){
			//$data = str_ireplace('#LinkTarget_','http://yishouce.com/book/' . $this->book_id . '/',);
			$data = $v['content_en'];
			$data = preg_replace('#<SPAN([\w\W]*?)>#i','',$data);
			$data = preg_replace('#</SPAN([\w\W]*?)>#i','',$data);
			obj('Book')->update(array('id'=>$v['id']),array('content_en'=>$data));
			//if($k>6) return;
		}
	}
	//增加前后的章节处理
	public function actionDeal(){
		$res = obj('Book')->select(array('book_id'=>$this->book_id),'id,section_id,content_en','id ASC');
		$count = count($res);
		foreach($res as $k=>$v) {
			$id = $v['id'];
			$next = (int) @$res[$k+1]['section_id'];
			$pre = (int) @$res[$k-1]['section_id'];
			
			preg_match('#<SPAN([\w\W]*?)</SPAN#',$v['content_en'],$match);
			$title = trim(str_replace('>','',$match[1]));
			obj('Book')->update(array('id'=>$id),array('next'=>$next,'pre'=>$pre,'title_en'=>$title,'title'=>$title,'item_name'=>'javascript'));
			//if($k>3) return;
		}
		echo $count;
		//var_dump($res);
	}
	
	//匹配章节并且入数据库
	public function actionNow(){
		$book = file_get_contents(BASE_DIR . 'protected/data/now_book.html');
		preg_match_all('#ID="LinkTarget([\w\W]*?)ID="LinkTarget#iu', $book ,$match1);
		
		$book2 = preg_replace('#ID="LinkTarget_#i','',$book,1);
		preg_match_all('#ID="LinkTarget([\w\W]*?)ID="LinkTarget#iu', $book2 ,$match2);
		
		$book_id = $this->book_id;
		echo count($match1[0]),'<br/>';
		echo count($match2[0]);
		exit;
		
		$next = 0;
		$pre = 0;
		foreach($match1[0] as $k=>$content) {
			foreach(array(1,2) as $kk){
				$var = 'match'.$kk;
				$content = ${$var}[0][$k];
				preg_match('#ID="LinkTarget_(\d*)#',$content, $m);
				$id = $m[1];//获取当前id
				$temp = preg_replace('#([\w\W]*?)>#','',$content,1);
				$substring = strrchr($temp,'<');
				$v = str_replace($substring,'',$temp); //div标签可能不对齐
				$re = obj('Book')->find(array('book_id'=>$book_id,'section_id'=>$id));
				if(!empty($re)) {echo "book: $book_id;$id 已有<br/>"; continue;}
				obj('Book')->insert(array('book_id'=>$book_id,'section_id'=>$id, 'content_en'=>$v));
			}
			//dump($v);
		//echo '<hr/>';
			//if($k>5) return;
		}
		return;
		echo count($match),'<br/>';
		echo count($match[0][0]),'<br/>';
		echo count($match[1]);
		//var_dump($match[0][0]);
		for($i=0; $i<5; $i++) {
			var_export($match[0][$i]);
			echo '============================================================';
		}
		// var_dump($match[0]);
	}
	
	//文件处理最先 去样式标签
	public function actionTag(){
		$source = BASE_DIR . 'protected/data/book1.html';
		$file = BASE_DIR . 'protected/data/now_book.html';
		
		$book = file_get_contents($source);
		$book = preg_replace('#<STYLE([\w\W]*?)</STYLE>#i','',$book);
		$book = preg_replace('#<META([\w\W]*?)>#i','',$book);
		$book = preg_replace('#<SPAN([\w\W]*?)>#i','',$book);
		$book = preg_replace('#</SPAN([\w\W]*?)>#i','',$book);
		//$book = preg_replace('# {2}#i',' ',$book);
		file_put_contents($file,$book);
	}
	
}